package moldat;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;

/**
 *
 * @author Marc Zoeller
 */
public class GeneExpression {

    private ArrayList<Organism> organismList = new ArrayList<Organism>();

    public GeneExpression(String geneName) throws FileNotFoundException, XMLStreamException, MalformedURLException, IOException {
        boolean isOrganismPart = false;
        Organism organism = null;
        String partName = null, upPvalue = null, downPvalue = null, upExperiment = null, downExperiment = null, nonDEExperiment = null;
        XMLInputFactory factory = XMLInputFactory.newInstance();
        URLConnection link = (new URL("http://www.ebi.ac.uk/gxa/api/vx?geneIs=" + geneName + "&format=xml")).openConnection();
        XMLEventReader parser = factory.createXMLEventReader(link.getInputStream());
        XMLEvent event;

        while (parser.hasNext()) {
            event = parser.nextEvent();
            if (event.isStartElement()) {
                StartElement start = event.asStartElement();
                if (start.getName().getLocalPart().equals("organism")) {
                    event = parser.nextEvent();
                    organism = new Organism(event.asCharacters().getData());
                    organismList.add(organism);
                    System.out.println("NEUER ORGANISM " + event.asCharacters().getData());
                }
                if (start.getName().getLocalPart().equals("ef")) {
                    event = parser.nextEvent();
                    isOrganismPart = event.asCharacters().getData().equals("organism_part");
                }
                if (isOrganismPart) {
                    if (start.getName().getLocalPart().equals("efv")) {
                        event = parser.nextEvent();
                        partName = event.asCharacters().getData();
                    }
                    if (start.getName().getLocalPart().equals("upPvalue")) {
                        event = parser.nextEvent();
                        upPvalue = event.asCharacters().getData();
                    }
                    if (start.getName().getLocalPart().equals("upExperiments")) {
                        event = parser.nextEvent();
                        upExperiment = event.asCharacters().getData();
                    }
                    if (start.getName().getLocalPart().equals("downExperiments")) {
                        event = parser.nextEvent();
                        downExperiment = event.asCharacters().getData();
                    }
                    if (start.getName().getLocalPart().equals("nonDEExperiments")) {
                        event = parser.nextEvent();
                        nonDEExperiment = event.asCharacters().getData();
                    }
                    if (start.getName().getLocalPart().equals("downPvalue")) {
                        event = parser.nextEvent();
                        downPvalue = event.asCharacters().getData();
                        if (partName == null || upPvalue == null || downPvalue == null)
                            ApplicationConfig.setImportDetails("Unable to find organism part.");
                        else
                            organism.addOrganismPart(partName, upPvalue, downPvalue, upExperiment, downExperiment, nonDEExperiment);
                    }
                }
            }
        }
    }

    public ArrayList<Organism> getOrganisms() {
        return organismList;
    }
}
